import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';

import '../../../common/style/AppSize.dart';
import '../../../common/style/color.dart';
import 'index.dart';

class ForgetPage extends GetView<ForgetController> {
  const ForgetPage({super.key});

  Widget _buildTextField({
    required TextEditingController controller,
    required String lable,

    String? hint,
    Widget? icon,
    bool obscure = false,
    TextInputType inputType = TextInputType.text,
    List<TextInputFormatter>? formatter,
    Widget? suffix,
  }) {
    return SizedBox(
      height: 48,
      child: TextField(
        controller: controller,
        obscureText: obscure,
        keyboardType: inputType,
        inputFormatters: formatter,
        decoration: InputDecoration(
          labelText: lable,
          hintText: hint,
          icon: icon,
          border: OutlineInputBorder(borderRadius: BorderRadius.circular(10)),
          contentPadding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
          suffixIcon: suffix,
        ),
      ),
    );
  }

  Widget _forget(ForgetController controller) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 50),
      child: Column(
        children: [
          // 验证手机号
          Obx(() {
            return controller.isPhoneVerify ? _inputNewPassword(controller) : _phoneCodeVerify(controller);
          }),
          const SizedBox(height: 30),

          // 忘记密码按钮
          Container(
            width: double.infinity,
            height: 50,
            child: Obx(() {
              return ElevatedButton(
                onPressed: () => controller.isPhoneVerify ? controller.forget() : controller.phoneCodeVerify(),
                child: Text(
                  controller.isPhoneVerify ? "修改密码" : "验证手机号",
                  style: TextStyle(color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold),
                ),
                style: ElevatedButton.styleFrom(backgroundColor: MyColors.main_color),
              );
            }),
          ),
        ],
      ),
    );
  }

  // 验证手机号
  Widget _phoneCodeVerify(ForgetController controller) {
    return Column(
      children: [
        // 手机号输入
        _buildTextField(
          controller: controller.phoneController,
          inputType: TextInputType.number,
          formatter: [LengthLimitingTextInputFormatter(11)],
          icon: SvgPicture.asset("assets/icon/phone.svg", width: 25, height: 25),
          lable: "手机号登录",
        ),

        const SizedBox(height: 30),

        _buildTextField(
          controller: controller.phoneCodeController,
          inputType: TextInputType.number,
          formatter: [LengthLimitingTextInputFormatter(6)],
          lable: "手机验证码",
          icon: SvgPicture.asset("assets/icon/phoneCode.svg", width: 25, height: 25),
          suffix: Obx(
            () => TextButton(onPressed: controller.isCounting ? null : controller.sendPhoneCode, child: Text(controller.getPhoneCode.value)),
          ),
        ),
      ],
    );
  }

  // 新密码
  Widget _inputNewPassword(ForgetController controller) {
    return Column(
      children: [
        // 新密码
        _buildTextField(
          controller: controller.newPasswordController,
          inputType: TextInputType.number,
          icon: SvgPicture.asset("assets/icon/password.svg", width: 25, height: 25),
          lable: "输入新密码",
        ),

        const SizedBox(height: 30),

        // 手机号输入
        _buildTextField(
          controller: controller.confirmNewPasswordController,
          inputType: TextInputType.number,
          icon: SvgPicture.asset("assets/icon/password.svg", width: 25, height: 25),
          lable: "输入新密码",
        ),
      ],
    );
  }

  // 主视图
  Widget _buildView(ForgetController controller) {
    return Column(
      children: [
        SizedBox(height: 50),
        Expanded(
          child: Container(
            width: double.infinity,
            child: _forget(controller),
            decoration: BoxDecoration(
              color: Colors.white, // 背景色
              borderRadius: BorderRadius.only(topRight: Radius.circular(AppSize.indicator)),
            ),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ForgetController>(
      init: ForgetController(),
      id: "forget",
      builder: (controller) {
        return Scaffold(
          resizeToAvoidBottomInset: false,
          appBar: AppBar(
            backgroundColor: MyColors.main_color,
            elevation: 0,
            title: Text("忘记密码", style: TextStyle(color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold)),
            centerTitle: true,
            leading: IconButton(onPressed: Get.back, icon: const Icon(Icons.arrow_back_outlined, size: 30, color: Colors.white)),
          ),
          body: Container(color: MyColors.main_color, child: SafeArea(bottom: false, child: _buildView(controller))),
        );
      },
    );
  }
}
